package com.mszlu.news.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.news.admin.mapper.SysUserMapper;
import com.mszlu.news.admin.model.params.UserParam;
import com.mszlu.news.admin.pojo.SysUser;
import com.mszlu.news.admin.service.SysUserService;
import com.mszlu.news.admin.vo.Result;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Result selectUser(UserParam userParam) {
        Page<SysUser> page=new Page<>(userParam.getPage(), userParam.getPageSize());
        IPage<SysUser> sysUserIPage=sysUserMapper.selectUser(page,userParam.getAccount(),userParam.getEmail(), userParam.getNickname(), userParam.getMobilePhoneNumber());
        return Result.success(sysUserIPage);
    }

    @Override
    public Result insertUser(UserParam userParam) {
        SysUser sysUser=new SysUser();
        sysUser.setAccount(userParam.getAccount());
        sysUser.setAdmin(userParam.getAdmin());
        sysUser.setAge(userParam.getAge());
        sysUser.setArea(userParam.getArea());
        sysUser.setDeleted(userParam.getDeleted());
        sysUser.setV(userParam.getV());
        sysUser.setAvatar(userParam.getAvatar());
        sysUser.setCreateDate(userParam.getCreateDate());
        sysUser.setDesign(userParam.getDesign());
        sysUser.setEmail(userParam.getEmail());
        sysUser.setHobby(userParam.getHobby());
        sysUser.setLastLogin(userParam.getLastLogin());
        sysUser.setMobilePhoneNumber(userParam.getMobilePhoneNumber());
        sysUser.setNickname(userParam.getNickname());
        sysUser.setPassword(DigestUtils.md5Hex(userParam.getPassword()));
        sysUser.setSalt(userParam.getSalt());
        sysUser.setSex(userParam.getSex());
        sysUser.setStatus(userParam.getStatus());
        sysUser.setWork(userParam.getWork());
        sysUserMapper.insert(sysUser);
        return Result.success("添加用户成功");
    }

    @Override
    public Result updateUser(UserParam userParam) {
        SysUser sysUser=new SysUser();
        sysUser.setId(userParam.getId());

        sysUser.setAccount(userParam.getAccount());
        sysUser.setAdmin(userParam.getAdmin());
        sysUser.setAge(userParam.getAge());
        sysUser.setArea(userParam.getArea());
        sysUser.setDeleted(userParam.getDeleted());
        sysUser.setV(userParam.getV());
        sysUser.setAvatar(userParam.getAvatar());
        sysUser.setCreateDate(userParam.getCreateDate());
        sysUser.setDesign(userParam.getDesign());
        sysUser.setEmail(userParam.getEmail());
        sysUser.setHobby(userParam.getHobby());
        sysUser.setLastLogin(userParam.getLastLogin());
        sysUser.setMobilePhoneNumber(userParam.getMobilePhoneNumber());
        sysUser.setNickname(userParam.getNickname());
        sysUser.setPassword(DigestUtils.md5Hex(userParam.getPassword()));
        sysUser.setSalt(userParam.getSalt());
        sysUser.setSex(userParam.getSex());
        sysUser.setStatus(userParam.getStatus());
        sysUser.setWork(userParam.getWork());
        sysUserMapper.updateById(sysUser);
        return Result.success("更新用户成功");
    }

    @Override
    public Result deleteUserById(Long userId) {
        sysUserMapper.deleteById(userId);
        return Result.success("删除用户成功");
    }

    @Override
    public Result UserSexCounts() {
        QueryWrapper<SysUser> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.select("count(sex) as sexCounts","sex");
        userQueryWrapper.groupBy("sex");
        List<SysUser> sysUserList=sysUserMapper.selectList(userQueryWrapper);
        return Result.success(sysUserList);
    }
}
